	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.12.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using data files in $dir: 
	****
	****  	wdi-original.xlsx
	**** 	V-Dem-CY-Full+Others-v111.dta
	****  	GWFglobal2020.dta
	****  	reign-regime.csv
	****  	Claassen-Support-Democracy.csv
	****  	esvp-johnson-wallack-2012.dta
	****  	Personalist-Parties-Final.xlsx
	****  	V-Dem-CPD-Party-V1.dta
	****
	*********************************************************************************
 

		
		capture log close
		log using Ch2-Appendix.log, replace

	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"

***************************************************************
**** Load, clean, and merge external data sets for merging ****
***************************************************************
			import excel "$dir\wdi-original", clear firstrow  /* downloaded 12.20.21 */
			gen n = _n
			drop if n>9310
			destring _all,replace ignore("..")
			rename Time year
			rename CountryName wdi_country
			rename CountryCode wdi_ccode
			gen country = wdi_country
			gen cowcode =.
			replace country ="Korea, Dem. Rep." if country=="Korea, Dem. Peoples Rep."
			qui do cowcodes
			tab wdi_country if cow==.
			drop if cow==.
			drop n TimeCode  country
			sum
			order cow year wdi*
			gen lpop = ln(Populationtotal)
			hist GDPpercapitaconstant2015US
			gen loggdppc = ln(GDPpercapitaconstant2015US)
			gen gdppc = GDPpercapitaconstant2015US^.5  
			sfrancia GDPpercapitaconstant2015US loggdppc gdppc  /* sqrt is more normal than raw or log */ 
			gen imr =sqrt(Mortalityrateinfantper100)
			sfrancia Mortalityrateinfantper100 imr
			tsset cow year
			gen gr = GDPpercapitagrowthannual
			tssmooth ma l12gr  = GDPpercapitagrowthannual, window(2 0 0)
			tssmooth ma l1gr  = GDPpercapitagrowthannual, window(1 0 0)
			gen oilgasrentsgdp =  ln(1+OilrentsofGDPNYGDPPET + NaturalgasrentsofGDPNY)
			gen mobilephone=sqrt(Mobilecellularsubscriptionsp/1000000)
			sort cow year
			save wdi-merge2,replace
	
		* VDem data * /* downloaded 4.06.2021 from https://www.v-dem.net/en/data/data/v-dem-dataset-v111/ */
			use V-Dem-CY-Full+Others-v111,clear
			keep country_name year COWcode v2x_polyarchy v2x_libdem v2x_partipdem v2x_freexp_altinf v2x_frassoc_thick ///
				v2x_clpol v2x_civlib v2x_clpriv v2x_clphy v2csreprss v2xlg_legcon v2dlencmps v2cltort v2clkill v2x_corr ///
				v2juhccomp v2jupack v2jupurge v2jupoatck v2jureform v2x_jucon v2juhcind  ///
				v2smgovfilprc* v2smgovsmcenprc* v2smgovsmmon* v2smgovsm* v2smgovshut* v2smgovsmalt* v2smgovdom v2smpardom v2smcamp ///
				v2smregapp  v2smpolhate v2smonper v2smorgavgact v2smonex v2smorgelitact v2caassemb v2cseeorgs v2casoe* ///
				v2smgovfilprc v2smgovsmcenprc v2smgovsmmon v2smgovsm v2smgovshut v2smgovcapsec v2smgovshutcap v2smgovfilcap v2smregcap ///
				e_civil_war e_migdppcln e_migdpgro e_wbgi_gee e_wbgi_pve e_v2xel_frefair_3C e_v2xcl_rol_3C ///
				v2xel_elecpres v2xel_elecparl v2xps_party v2x_regime v2exl_legitlead* v2exl_legitperf v2exl_legitideol ///
				v2cacamps* v2smpolsoc* v2elpubfin_ord ///
				v2stfisccap v2clrspct v2x_pubcorr v2cltrnslw v2stcritrecadm v2stcritapparm v2strenadm v2strenarm v2peasbepol v2peasjpol ///
				v3stcensus v3stnatbank v3ststatag v3ststybcov v3ststybpub ///
				v2lpname v2elvotlrg v2elloelsy v2ellostlg v2ellostsl v2ellostss v2elparlel v2elthresh ///
				v2exrescon v2regimpgroup v2regsupgroups* v2pscohesv v2lginvstp v2lgotovst v2lgoppart ///
				v2exbribe v2exembez v2x_execorr v2cagenmob v2cademmob v2caautmob v2caviol v2csprtcpt ///
				v2elaccept v2elturnhog v2elturnhos v2eltvrexo v2elpeace v2eldonate v2elpubfin v2el*
			rename country_name vdem_country
			rename COWcode cowcode
			recode cow (679=678) if year>1990
			tab vdem_country if cow==.
			drop if cow==.
			tab vdem_country if cowcode==99999   /* cases in our sample */
			drop if cowcode==99999
			replace v2x_clphy= (v2x_clphy*-1) +1/* flip scale so it measures repression instead of human rights respect */
				* Impersonal state bureaucracy *
				gsem  (PER-> v2clrspct v2stcritrecadm v2strenadm v2x_pubcorr v2cltrnslw,fam(gaussian)link(id) ///
					var(PER@1)vce(cluster cowcode))
				predict vburcap,ebmeans latent se(se_vburcap)
				* Polarization *				
				desc v2cacamps v2smpolsoc			
				gen polarization=v2cacamps
			tsset cow year
			sort cowcode year
			global vA="v2x_polyarchy v2x_libdem v2x_partipdem v2x_clpriv v2x_freexp_altinf v2x_frassoc_thick v2elpubfin_ord"
			global vB="v2lpname v2elvotlrg v2elloelsy  v2elparlel v2elthresh v2ellostsl v2ellostss v2xps_party v2pscohesv"
			global vC=" v2exrescon v2casoe_0 v2casoe_1 v2exbribe v2exembez v2x_execorr v2lginvstp v2lgotovst v2lgoppart v2xlg_legcon"
			global vD="v2x_clpol v2x_clphy v2x_civlib v2caassemb v2cseeorgs v2csprtcpt v2csreprss v2smgovdom v2smpardom"
			global vE="v2cagenmob v2cademmob v2caautmob v2caviol vburcap polarization v2x_jucon v2juhcind v2eldonate v2elpubfin"
			local var = "$vA $vB $vC $vD $vE"
			foreach v of local var {
				tsset cow year
				gen l1`v'=l1.`v' 
				gen l2`v'=l2.`v' 
				gen l3`v'=l3.`v' 
				gen l4`v'=l4.`v' 
			}
			
			sum year cow
			xtset cow year
			alpha v2clkill v2cltort if year>=1990, item std gen(vkill)
			replace vkill = vkill*-1
			
			gen vdem_democracy=v2x_regime>=2 & v2x_regime!=.
			tab vdem_democracy
			tsset cow year
			gen l1vdem_democracy = l.vdem_democracy
			gen enddem = vdem_democracy==0 & l1vdem_democracy==1
			
			 ** 
			 recode cow (679=678) if year==1990 & vdem_country=="Yemen"
			 recode cow (260=255) if year>1945 & year<=1990 & vdem_country=="Germany"
			 recode cow (316=315) if year==1993 & vdem_country=="Czech Republic"
 
			* Merge GWF data *
			sort cow year
			merge cow year using "GWFglobal2020.dta"
			sum year if v2x_polyarchy~=.
			sum year if gwf_country ~=""
			tab _merge if year>=1988  & year<=2019  /* All GWF data in the data set */
			drop if _merge==1
			rename _merge merge1
			sum v2smgovfilprc v2smgovsmcenprc v2smgovsmmon v2smgovsm v2smgovshut v2smgovcapsec ///
				v2smgovshutcap v2smgovfilcap v2smregcap if year<2000
			keep if year>=1988 & year<=2020
			
 			xtset cow year
			sort cow year
			merge cow year using wdi-merge2
			tab _merge
			rename _merge merge2
			tab gwf_country if merge2==1 /* note that South Sudan, Swaziland, and Taiwan (2000) are missing from WDI */
			tab wdi_country if merge2==2
			drop if merge2==2
			replace cow =626 if gwf_country=="South Sudan"
			xtset cow year
			gen l1gdp =l.gdp
			sort cowcode year
			merge cowcode year using fariss-merge
			tab _merge  /* no issues merging */
			drop if _merge==2
			rename _merge merge4
			sort cowcode year
			merge cowcode year using prio-mergeB
			tab _merge
			rename _merge merge5
			drop if year<1988
			tab merge5
			sum year if merge5==2  /* all in 2018 */
			drop if merge5==2
			recode prio_conflict* prio_lconflict* (.=0) if year<=2018
			sort cowcode year
 			save master,replace
			
			insheet   using "reign-regime.csv",clear
			gen sdate = date(gwf_startdate, "MDY")
			gen edate = date(gwf_enddate, "MDY")
 			gen syear = year(sdate)
			gen eyear = year(edate)
			gen duration = eyear-syear +1
			expand duration
			gen n=_n
			egen m = min(n),by(gwf_casename)
			gen year =syear+1 if m==n
			sort gwf_casename year
			replace year = year[_n-1]+1 if year==.
			drop if year>eyear & year<=2019 & gwf_enddate~="12/31/2019"
			drop if year==2020 & gwf_enddate~="12/31/2019"
			keep if year>1987 & year<=2020
			recode cowcode (679=678) if year>=1991
			recode cowcode (316=315) if year==1993
			recode cowcode (260=255) if year<=1990
			local var = "country startdate enddate regimetype casename"
			foreach v of local var {
				rename gwf_`v' reign_`v'
			}
			sort cow year
			xtset cowcode year
			merge cow year using master
			tab _merge
			tab gwf_country if _merge==2  /* Taiwan not in reign data */
			drop if _merge==1
			rename _merge merge6
			drop sdate edate syear eyear duration n m
			sort cowcode year
			save master, replace
			
			insheet using "Claassen-Support-Democracy.csv",clear  /* 1987--2017 coverage */
			replace supdem ="." if supdem=="NA"
			replace lsupdem="." if lsupdem=="NA"
			destring supdem lsupdem,replace
			rename country supdem_country
			sum
			xtset cowcode year
			gen l1supdem = lsupdem
			gen l2supdem = l.lsupdem
			gen l3supdem = l2.lsupdem
			gen l4supdem = l3.lsupdem
			
			sort cowcode year
			merge cowcode year using master
			tab _merge if  year<=2017 & (gwf_regime=="democracy" | gwf_regime=="provisional")
			tab gwf_country if _merge==2 &  year<=2017 & (gwf_regime=="democracy" | gwf_regime=="provisional")
			drop if _merge==1
			rename _merge merge7
			sort cowcode year
			save master,replace
		
			use "esvp-johnson-wallack-2012.dta",clear
			gen cowcode = .
			qui do cowcodes
			tab country if cowcode==.
			replace cowcode = 484 if country=="Congo (Republic of)"
			replace cowcode = 490 if country=="Congo, Democratic Repulic of"
			replace cowcode = 255 if country=="Germany (Federal Republic of)"
			replace cowcode = 732  if country=="Korea (South, Republic of Korea)"
			replace cowcode = 713 if country=="Taiwan (Republic of China)"
			replace cowcode = 731 if shcode==205
			replace cowcode = 343 if shcode==240
			tab country if cowcode==.
			drop if cowcode==.
			sort cowcode year
			merge cowcode year using master
			tab _merge
			rename _merge merge10
			xtset cowcode year
			sort cowcode year
			save master,replace
 			

***************************************************
************* A: Personalist Party data ***********
***************************************************

	*****************************
	*** Import and clean data ***
	*****************************
	import excel using "$dir\Personalist-Parties-Final",clear firstrow  
	drop  *_s electing_p_prior_elect_national_
	sum year
 	
	* List and drop technocratic appointments *
		egen tag=tag(current_leader) if electing_p_name=="Technocrat appointment"
		list country current_leader if tag==1
		drop if electing_p_name=="Technocrat appointment"
		drop tag
		
		tab current_p_create,m
		tab electing_p_create,m
		tab current_p_name if current_p_create==9  /* These are the true independents who never form a party while in office */
		tab current_p_create if current_p_name=="Technocrat appointment"  /* These are technocrats who don't have parties */
		tab electing_p_name if electing_p_create==9 
		tab electing_p_create if electing_p_name=="Technocrat appointment"  /* These are technocrats who don't have parties */
		tab electing_p_create if electing_p_name=="N/A"
		list current_p_name country year current_leader if electing_p_name=="N/A", noobs clean
		
		tab electing_p_name if electing_p_merge==9
		tab electing_p_merge_month if electing_p_merge==9
		tab electing_p_merge_year if electing_p_merge==9
		tab electing_p_founding_month if electing_p_merge==9
		tab electing_p_founding_year if electing_p_merge==9
 	    	
	******************
	*** Clean data ***
	******************
		recode current_p_create electing_p_create (9=3)  /* no party==sole party creation */
		recode current_p_create current_p_leader_family current_p_creator_family ///
		current_p_prior_elect_local current_p_prior_appt_local current_p_prior_elect_nati ///
		current_p_prior_appt_na current_p_prior_national_p electing_p_create ///
		electing_p_leader_family electing_p_creator_family electing_p_prior_elect_loc ///
		electing_p_prior_appt_l electing_p_prior_elect_nat electing_p_prior_appt_n ///
		electing_p_prior_national_p prior_p_1_create prior_p_1_leader_family ///
		prior_p_1_creator_family prior_p_1_prior_elect_loca prior_p_1_prior_appt_lo ///
		prior_p_1_prior_elect_nati prior_p_1_prior_appt_na prior_p_1_prior_national_p ///
		prior_p_2_leader_family prior_p_2_create prior_p_2_creator_family prior_p_2_prior_elect_loca ///
		prior_p_2_prior_appt_lo prior_p_2_prior_elect_nati prior_p_2_prior_appt_na ///
		prior_p_2_prior_national_p (9=.)
	 
	******************************
	*** Variable construction  ***
	******************************	 
		* Recode missing to zero *
		recode prior_p_1_prior_elect_loca prior_p_1_create prior_p_1_leader_family ///
			prior_p_1_creator_family  ///
			prior_p_1_prior_appt_lo prior_p_1_prior_elect_nati ///
			prior_p_1_prior_appt_na prior_p_1_prior_national_p (.=0)
		recode electing_p_prior_elect_loc electing_p_prior_appt_l ///
			electing_p_prior_elect_nat electing_p_prior_appt_n ///
			electing_p_prior_national_p (.=0)
		recode electing_p_leader_family electing_p_creator_family (.=0)
		
		* Short names *
		gen localelect = electing_p_prior_elect_l
		gen localappt = electing_p_prior_appt_l 
		gen natelect = electing_p_prior_elect_n
		gen natappt = electing_p_prior_appt_n
		gen natparty = electing_p_prior_national_p
			    
		* Binary party creation variable, not merger *
		gen create = electing_p_create==1 | electing_p_create==3 | electing_p_name=="N/A"  
  
		* Prior independent grouped together *
		recode indep* (9=0)
		sum indep*
		gen priorindep = indep_prior_local_elect==1 |  indep_prior_local_appt==1 | ///
			indep_prior_national_elect==1 | indep_prior_national_appt==1 | ///
			indep_prior_national_defea==1		
		
		* Leaders *
		egen lid = group(leader_id)
		egen minyr = min(year),by(lid)

		* Party age *
		gen partyage = current_leader_start_year-electing_p_founding_year if electing_p_founding_year>0
		browse country year current_leader current_p_name current_p_create if electing_p_founding_year==0
		recode partyage (.=0) 
		replace partyage=0 if partyage<0
		gen lnpartyage =ln(1+partyage)
 
		* Experience in the party *
		replace first_year_exp="." if first_year_exp=="N/A"
		replace year_date_position="." if year_date_position=="N/A"
		destring year_date_position first_year_exp,replace
		sum year year_date_position first_year_exp
		gen timeparty = current_leader_start_year-first_year_exp
		replace timeparty =0 if timeparty==. | timeparty<0
		qui centile partyage if create==0,centile(50) /* old parties */
		local cut1 = r(c_1)
		qui centile timeparty if create==0,centile(33.33)  /* not super new party experience */
		local cut2 = r(c_1)
		gen partyexp = partyage>`cut1' & timeparty>=10
		tab partyexp create
		
		* Flip scale so 1 \equiv more personalist *
		recode localappt natelect natappt natparty localelect partyexp (1=0) (0=1)
		
		* Set sample *
		gen cowcode = ccode
		drop if cowcode==.
		sort cowcode year
		xtset cowcode year
	 
		* Show that family variables lie along a distinct dimension: 
		* should not be modeled with SEM-IRT, which assumes one dimension, which is create party *
		factor electing_p_creator_family electing_p_leader_family ///
			natelect natappt natparty localappt create localelect priorindep partyexp,mineigen(1)
		rotate, oblique promax(3)
		screeplot,tit(Eigen values for 8 items plus 2 family items)saving(h1.gph,replace)
		loadingplot,saving(h2.gph,replace) note("")
		gr combine h1.gph h2.gph,xsize(8)
 
		* Variables for 8-item IRT *
		factortest natelect natappt natparty localappt create localelect priorindep partyexp
		factor     natelect natappt natparty localappt create localelect priorindep partyexp,mineigen(.25)  
 		screeplot,tit(Eigen values for 8 items)saving(h1.gph,replace)xlab(0(2)8)
		loadingplot,saving(h2.gph,replace)
		gr combine h1.gph h2.gph,xsize(8)
  		erase h1.gph
		erase h2.gph

		* IRT *
		sum create natelect natappt natparty localelect localappt priorind partyexp
		polychoric create natelect natappt natparty localelect localappt priorind partyexp
		tab localappt create
		tab partyexp create
		tab localappt priorind
		corr create natelect natappt natparty localelect localappt priorind partyexp
		alpha  create natelect natappt natparty localelect localappt priorind partyexp,item detail std

		 /* local elect contributes the least info */
 		irt (2pl create natelect natappt natparty localelect localappt priorind  partyexp),vce(cluster lid) 
		irtgraph iif (create,lcol(red)) (partyexp,lcol(cyan)lpat(solid))  ///
			(natelect,lcol(blue*.5)lpat(solid)) (natappt,lcol(blue*1.5)lpat(dash)) ///
			(priorind,lcol(magenta)lpat(solid)) (natparty,lcol(orange)lpat(solid))  ///
			(localappt,lcol(green*0.5)lpat(solid)) (localelect,lcol(green*1.5)lpat(dash)) 
			
			*** IRT info plots ***
			gen a1 = "&"
			gen a2 = "&"
			gen a3 = "&"
			gen a4 = "&"
			gen a5 = "&"
			gen a6 = "&"
			gen b = "\\"
			gen discr=.
			gen diff=.
			gen hi_disc = .
			gen lo_disc = .
			gen hi_diff = .
			gen lo_diff = .
			gen xn = _n
			gen var = ""
			irt (2pl create natelect natappt natparty localelect localappt priorind partyexp),vce(cluster lid) 
			mat e = e(b)
			mat var = r(V)
			forval i = 1(2)15 {
				qui replace discr=e[1,`i'] if xn==`i'
				qui local v = sqrt(var[`i',`i']) 
				qui replace hi_disc =  e[1,`i'] + 1.96*`v' if xn==`i'
				qui replace lo_disc =  e[1,`i'] - 1.96*`v' if xn==`i'
			}
			forval i = 2(2)16 {
				local j = `i'-1
				qui replace diff= -1*(e[1,`i']/e[1,`j']) if xn==`i'-1
				local v =  sqrt(var[`i',`i'])
				qui replace hi_diff = (-1*(e[1,`i']/e[1,`j'])) + 1.96*`v' if xn==`i'-1
				qui replace lo_diff = (-1*(e[1,`i']/e[1,`j'])) - 1.96*`v' if xn==`i'-1
			}
			qui replace var = "create" if xn==1
			qui replace var = "natelect" if xn==3
			qui replace var = "natappt" if xn==5
			qui replace var = "natparty" if xn==7
			qui replace var = "localelect" if xn==9
			qui replace var = "localappt" if xn==11
			qui replace var = "priorind" if xn==13
			qui replace var = "partyexp" if xn==15
			list var a1 hi_disc  discr   lo_disc a4  hi_diff   diff   lo_diff  if xn<=16,clean noobs
			twoway (rspike hi_disc lo_disc xn  if xn<=16,horizontal xline(0) tit(Discrimination)) ///
				(scatter xn disc if xn<=16,msym(P)saving(h1.gph,replace)legend(off)ytit(Item)xtit(Information) ///
				ylab(1 "create party" 3 "national elected" 5 "national appointed" 7 "party leadership" ///
				9 "local elected" 11 "local appointed" 13 "prior indep." 15 " party exper."))
			twoway (rspike hi_diff lo_diff xn if xn<=16,xline(0)tit(Difficulty)horizontal) ///
				(scatter xn diff if xn<=16,msym(P)saving(h2.gph,replace)legend(off)ytit(Item)xtit({&theta}) ///
				ylab(1 "create party" 3 "national elected" 5 "national appointed" 7 "party leadership" ///
				9 "local elected" 11 "local appointed" 13 "prior indep." 15 " party exper."))
			gr combine h1.gph h2.gph
			erase h1.gph
			erase h2.gph
			drop a1 a2 a3 a4 a5 a6 
		gr export "$dir\golden\Ch2A-iif.pdf",as(pdf)replace 
		estat ic
		predict persparty,latent ebmeans se(se_pers)
		gsem (PER->localappt localelect create natelect natappt natparty priorindep,logit var(PER@1)) ///
			(PER->partyage timeparty,reg var(PER@1)),vce(cluster lid)
		predict persparty_mix,latent ebmeans
		estat ic
		hist persparty   
		sum persparty		
		replace persparty =(persparty+abs(r(min)))/(abs(r(min)) + r(max))
		sum persparty		
		sort cow year
		corr persparty*
		 
		save pers-temp,replace
		
***********************************************************************
**** Merge persparty measure with VDem parties data for validation ****
***********************************************************************
		
		***************************************************************
		**** Expand VDem party data into country-party-year format ****
		***************************************************************
		 use V-Dem-CPD-Party-V1,clear
		 drop if v2paid==.
		 egen cyvote = sum(v2pavote),by(country_name year)
		 list country_name year if cyvote>150 & cyvote ~=.,clean noobs
		 drop if cyvote>150 & cyvote ~=.
		 gen yr = year

		 	 * Illiberalism *
		 gen illiberal = v2xpa_illiberal
		 egen pminyear =min(yr) if v2xpa_illiberal~=.,by(v2paid)
		 gen oilliberal = illiberal if yr==pminyear
		 egen i_illiberal = max(oilliberal),by(v2paid)
 		 
		 * V-Party measure of populism *
		 gen populism =v2xpa_popul
		 gen opopulism = populism if yr==pminyear
		 egen i_populism = max(opopulism),by(v2paid)

		 * Votes and party personalism *
		 sum v2pavote
		 gen vote = v2pavote/100
		 recode vote (0=.)
		 gen ppers = v2paind
		 qui sum ppers
		 replace ppers = ppers+abs(r(min))
		 qui sum ppers
		 replace ppers = ppers/r(max)
		 sum ppers
		 replace ppers = ppers>.558 if ppers~=.
		 tab ppers
 
		 egen maxc  =max(year),by(country_name)
		 gen maxyrc = maxc==year
		 sort v2paid year
		 bysort v2paid: gen diff =  year[_n+1]-year
		 
		 * Carry forward to 2020 *
		 replace diff = 2020-year+1 if maxyrc==1
		 sort v2paid year
		 bysort v2paid:replace diff = diff[_n-1]+1 if diff==. & v2paid==v2paid[_n-1]
 		 replace diff=2 if year==2019
		 
		 * Carry forward from last election for the Party ID *
		 egen max  =max(year),by(v2paid)
		 gen maxyr =max==year
		 replace diff = 12 if maxyr==1
		 drop max maxyr
	  
		 expand diff
		 sort country_name v2paid year
		 bysort country_name v2paid:replace yr = yr[_n-1]+1 if year==year[_n-1] & v2paid==v2paid[_n-1]  
		 replace diff=1
		 
		* Carry backwards one year from first election for the Party ID *
		 sort v2paid year
		 egen min  =min(yr),by(v2paid)
		 gen minyr =min==yr
		 replace diff = 6 if minyr==1
		 expand diff
		 drop min minyr
		 sort country_name v2paid yr
		 bysort country_name v2paid:replace yr = yr-1 if yr==yr[_n+1] & v2paid==v2paid[_n+1] & diff>=2 
 		 sort country_name v2paid yr
		 bysort country_name v2paid:replace yr = yr-1 if yr==yr[_n+1] & v2paid==v2paid[_n+1] & diff>=2 
		 sort country_name v2paid yr
		 bysort country_name v2paid:replace yr = yr-1 if yr==yr[_n+1] & v2paid==v2paid[_n+1] & diff>=2 
 		 sort country_name v2paid yr
		 bysort country_name v2paid:replace yr = yr-1 if yr==yr[_n+1] & v2paid==v2paid[_n+1] & diff>=2 
		 sort country_name v2paid yr
		 bysort country_name v2paid:replace yr = yr-1 if yr==yr[_n+1] & v2paid==v2paid[_n+1] & diff>=2 
		 
		 drop opopulism oilliberal  
 		 
		 * Generate lags *
		 xtset v2paid yr
		 forval i = 1/4 {
			local var = "illiberal populism ppers"
			foreach v of local var {
				gen l`i'`v'=l`i'.`v'
			}
		 }
		 
		 
		 drop year
		 rename yr year
		 sort v2paid year
		 save vdem-parties-merge,replace		 
		
		 *****************************************************************************
		 *** Merge VDem country-party-year data with personalist country-year data ***
		 *****************************************************************************
		use pers-temp,clear
		keep if year>=1990  
		gen v2paid = electing_p_id
		/*
		 We have double-checked these parties, which do not appear in the VDem parties data set:
		(1) Nepali Congress (Democratic)-NC (D) is not the same (in 2005) as the Nepali Congress (ID 3756)
		(2) Unified Communist Party of Nepal (Maoist)-UCPN (M) is not the same as 
			Communist Party of Nepal (Unified Marxist-Leninist)-CPN (UML) (ID 3755)
		(3) Sri Lanka Podujana Peramuna (SLPP) postdates the VDem parties data set: 
			The SLPP was effectively re-launched in November 2016 by Mahinda Rajapaksa; 
			The Sri Lanka National Front (SLNF) was minor party renamed Our Sri Lanka Freedom Front (OSLFF) 
			in 2015; However, OSLFF was relaunched by Mahinda Rajapaksa in 2016 as the Sri Lanka Podujana 
			Peramuna (SLPP) and became the home for Rajapaksa supporters and Rajapaksa-faction members 
			of the United People's Freedom Alliance (UFPA) and Sri Lanka Freedom Party (SLFP), both
			of which had backed Mahinda Rajapaksa in the 2005 election.
		(4) Shekhar broke with the Janata Dal Party on November 5, 1990 and formed the 
			Janata Dal-Socialist faction. When selected PM in in Nov 1990, Shekhar was backed by the INC
			but his party was the Janata Dal-Socialists not the rump Janata Dal Party (ID 1207)
		(5) AKFM-Fanavaozana split from AKFM in 1989 when AKFM refused to nominate Andriamanjato as its
			presidential candidate. In 1990, AKFM-Fanavaozana joined with other opposition groups 
			to form the Comité des Forces Vives (ID 5368) and backed Zafy in the 1993 presidential election.
		(6) New Force for Madagascar (Hery Vaovao ho an'i Madagasikara–HVM) is not included in VDem-Parties; 
			PartyFacts group HVM and TGV together as other (ID 5207)
		(7) Madagascar	2019 Rajoelina	TGV not in VDem-Parties but has a grouped PartyFacts ID (5207)
		(8) Sovereign Communists/Party of Power in Urkaine was the grouping of supporters for Kravchuk; 
			These groups were not formally registered political parties. VDem
			does not record this grouping as political party during this period.
		(9) National Reconstruction Party (PRN) in Brazil 1991-1992 has a PartyFacts ID (4410) but
			is not included in the VDem-Parties data.
		(10) Peasant Response Party - Haiti/Martelly 2011 - has a PartyFactsID (6069) but not in VDem-Parties.
		(11) Ukraine's Inter-Regional Bloc of Reforms (MRBR) (PartyFacts ID 2227) is not in VDem-Parties.
		(12) Ukraine's Sovereign Communists/Party of Power has neither a PartyFacts ID nor is in VDem-Parties.
		(13) (North) Macedonia's Social Democratic Union of Macedonia (PartyFacts ID 1508) 
			 is not in VDem-Parties.
		(14) Slovenian Christian Democrats (Slovenski krščanski demokrati, SKD, PartyFactsID 644) is
			 not in VDem-Parties.
		(15) Lithuania's Sajudis Party/Sajudzio koalicija (1991) has PartyFacts ID 743 but
			 is not in VDem-Parties.
		*/
		
		sort v2paid year
		merge v2paid year using vdem-parties-merge, 
		tab _merge
		drop if _merge ==2
		egen tag = tag(country current_leader) if _merge==1
		list country year current_leader electing_p_id electing_p_name if tag==1, noobs clean
		drop _merge
		sort cowcode year
		xtset cowcode year

		sort cowcode year
		merge cowcode year using master
		tab _merge
		list country year if _merge==1,clean noobs
		drop if _merge==1
		drop _merge

		
		xtset cowcode year
		gen lv2paind= l.v2paind 
		gen legit=  l.v2exl_legitlead  /* lag 1 year to ensure not endogenous to backsliding event */
		gen olv2paind= l.v2paind_ord 
		gen olegit=  l.v2exl_legitlead_ord  /* lag 1 year to ensure not endogenous to backsliding event */
		recode gwf_back (.=0) if country=="Luxembourg"

		gen type = reign_regimetype
		sort cowcode year
		replace type = reign_regimetype[_n-1] if type~="presidential" & type~="parliamentary"
		sort cowcode year
		replace type = type[_n-1] if type~="presidential" & type~="parliamentary"
		
		
		gen demsample= year>=1991 & year<=2020 & gwf_back~=. & (gwf_regime=="democracy" | gwf_regime=="provisional")
		keep if (gwf_regime=="democracy" | gwf_regime=="provisional") & year>=1991
		keep if current_leader~=""
 
		* Rescale on 0,1 *
		qui sum persparty  
		replace persparty =(persparty+abs(r(min)))/(abs(r(min)) + r(max))
		 
		* Take a look *
		xtsum  	 persparty   
		sfrancia persparty  
		
		***********************
		* Internal validation *
		***********************
			  * LOO *
			 qui alpha create natelect natappt localappt priorindep localelect natparty partyexp,gen(a) 
			 qui alpha natelect natappt localappt priorindep localelect natparty partyexp,gen(a1) 
			 qui alpha create natappt localappt priorindep localelect natparty partyexp,gen(a2) 
			 qui alpha create natelect natappt priorindep localelect natparty partyexp,gen(a3) 
			 qui alpha create natelect natappt localappt localelect natparty partyexp,gen(a4) 
			 qui alpha create natelect natappt localappt localelect natparty partyexp,gen(a5)  
			 qui alpha create natelect natappt localappt priorindep natparty partyexp,gen(a6) 
			 qui alpha create natelect natappt localappt priorindep localelect partyexp,gen(a7)   
			 qui alpha create natelect natappt localappt priorindep localelect,gen(a8)   

			 corr a a1 a2 a3 a4 a5 a6 a7 a8 
			 drop a a1 a2 a3 a4 a5 a6 a7 a8
			
			* Split sample reliability *
			global pparty="persparty"
			** Internal reliability checks **
			gen early=year<=2006 if $pparty~=.
			tab early if $pparty~=.
			replace gdp=gdp/1000
			gen poor =gdp<  59.17128 if $pparty~=.
			tab poor if $pparty~=.
			gen lopop = lpop<16.16824 if $pparty~=. & lpop~=.
			tab lopop if $pparty~=.
			gen new =gwf_duration<19 if $pparty~=.
			tab new if $pparty~=.
			gen hipartyinst= v2xps_party>=.784  if $pparty~=. & v2xps_party~=.
			tab hipartyinst if $pparty~=.
			gen dtype = type=="presidential"
			tab dtype if $pparty~=.
			gen eur=cowcode>=200 & cowcode<400
			tab eur
			gen afr  = cowcode>=400 & cowcode<=630
			tab afr if $pparty~=.
			sum dtype early poor lopop new hipartyins eur afr
			matrix m = J(8,1,.)
			local var = "dtype early poor lopop new hipartyinst eur afr" 
			local i =1
			foreach v of local var {
				qui gsem (PER->create natelect natappt localappt localelect natparty priorindep partyexp  ///
					if `v'~=. & $pparty~=.,logit var(PER@1) group(`v'))
				predict `v'pparty,latent ebmeans
				spearman `v'pparty $pparty
				matrix j = r(rho)
				local s = round(j[1,1],.01)
				matrix m[`i',1] =`s'
				local i= `i' + 1
				drop `v'pparty
			}
			mat rownames m = Pres/parl Pre/post2005 Income Population DemocracyAge PartyInst Europe Africa
			matrix list m
			plotmatrix, m(m)c(ltblue)legend(off)title(Internal validation,size(medium))freq split(0(.01)1)  ///
				xsize(2)ysize(5)xlab(1 "Personalist party") ylab(0 "Pres/parl" -1 "Pre/post 2005" -2 `""High/low" "income ""' ///
				-3 `""Large/small" "population ""' -4 `""New/old  " "democracy""'  ///
				-5 `""High/low party   " "institutionalization""' -6 `""Within/outside" "Europe     ""' ///
				-7 `""Within/outside" "Africa     ""',labsize(med))  
			drop dtype early poor lopop new hipartyinst eur afr
		
	 
		***********************
		* External validation *
		***********************
		   * correlations with external measures*
			matrix m = J(6,6,.)
			matrix list m
			local dimensions = "create persparty v2paind v2exl_legitlead v2xps_party v2x_polyarchy"
			local i=1
			foreach t of local dimensions {
				local j = 1
				local klass = "create persparty v2paind v2exl_legitlead v2xps_party  v2x_polyarchy"
				foreach k of local klass {
					spearman `t' `k' if persparty~=.
					matrix j = r(rho)
					local s = round(j[1,1],.01)
					matrix m[`i',`j'] =`s'
					local j= `j' + 1
				}
				local i = `i'+1
			}
			matrix list m
			plotmatrix, m(m) c(gs10) legend(off) title(Correlation matrix,size(medium)) freq split(0(.01)1)  ///
				xsize(3)ysize(2)xlab(1 "Create party" 2 "Party pers." 3 "V-Party Pers." ///
				4 "V-Legit leader" 5 "V-Party inst." 6 "V-Democracy",labsize(vsmall)) ///
				ylab(0 "Create party" -1 "Party pers." -2 "V-Party Pers." ///
				-3 "V-Legit leader" -4 "V-Party inst." -5 "V-Democracy",labsize(vsmall))  
			gr export "$dir\golden\Ch2A-ExCorr.pdf",as(pdf)replace 
		drop if persparty==.
		
		* Generate some system variables *
		gen priormil = gwf_priorreg=="military" | gwf_priorreg=="indirect military" | gwf_priorreg=="milpersonal"
		gen pres=type=="presidential" if type~=""
		tab pres type,m	
		
		*** Time periods ***
		gen period = year<=1995
		replace period = 2 if year>1995 & year<=2000
		replace period = 3 if year>2000 & year<=2005
		replace period = 4 if year>2005 & year<=2010
		replace period = 5 if year>2010 & year<=2015
		replace period = 6 if year>2015  
		gen xperiod1 = year<=1995
		gen xperiod2 = year>1995 & year<=2000
		gen xperiod3 = year>2000 & year<=2005
		gen xperiod4 = year>2005 & year<=2010
		gen xperiod5 = year>2010 & year<=2015
		gen xperiod6 = year>2015  
		tab period
		global pparty="persparty"
		
		* Initial levels of democracy and party institutionalization *
  		gen ld = ln(1+gwf_duration)
 		gen odem = l1v2x_polyarchy if minyr==year
 		replace odem = l1v2x_polyarchy if minyr+1==year & odem==.
		replace odem =v2x_polyarchy if minyr==year  & odem==.
		egen ivdem = max(odem),by(lid) 
		replace ivdem = l1v2x_polyarchy if ivdem==.
		gen opi = l2v2xps_party if minyr==year
		replace opi = l1v2xps_party if minyr==year & opi==. &  l1v2xps_party~=.
		egen ipi = max(opi),by(lid)
		
		* Electoral system data from VDem: carry forward from past election *
		local var ="v2elloelsy v2elparlel"
		foreach v of local var {
			forval i = 1/4 {
				replace `v'=l`i'`v' if `v'==.
			}
			forval i = 1/8 {
				replace `v'=l`i'.l4`v' if `v'==.
			}
			replace `v'=f.`v' if `v'==.
			replace `v'=f2.`v' if `v'==.
			replace `v'=f3.`v' if `v'==.
			
		}
	 
			recode v2elparlel (.=1) if country=="Argentina" | country=="Armenia"
			recode v2elparlel (.=0) if country=="Malaysia" | country=="Gambia"		
			forval i = 1/4 {
				replace v2ellostsl=l`i'v2ellostsl if v2ellostsl==.
				replace v2ellostss=l`i'v2ellostss if v2ellostss==.
			}
			
			forval i = 1/8 {
				replace v2ellostsl=l`i'.l4v2ellostsl if v2ellostsl==.
				replace v2ellostss=l`i'.l4v2ellostss if v2ellostss==.
			}
			 

		* Sample *
	 	* GNB 2012: non-personalist leader dies a naturual death 9 Jan 2012; 
		* interim president takes over and their is a coup in April 
		* drop the interim leader who is unelected
		gen GNB = year==2012 & country=="Guinea Bissau"  
		keep if current_leader~="" & (gwf_democracy==1  | gwf_provisional==1)
		
		gen leadertimeinpower = 0 if year==minyr
		sort lid year
		bysort lid: replace leadertimein=leadertimein[_n-1]+1 if leadertimein==. & lid==lid[_n-1]
		
		* Political-geographic region *
		recode e_regionpol_6C (2=4) if country=="Guinea Bissau"
		egen pregion = max( e_regionpol_6C),by(cowcode)
		tab pregion, m
		recode pregion  (.=5) /* Afghanistan */
		
 		tsset cowcode year
		sort cowcode year
		save pers-use,replace
	 
		 erase pers-temp.dta
		 erase master.dta
		 erase wdi-merge2.dta		 
	
	************** THE END ******************

	log close
	
 
	
